﻿using Files_Upload.API.Dto;
using Files_Upload.application;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;

namespace Files_Upload.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ReportController : ControllerBase
    {
        private readonly IReportService _reportService;

        public ReportController(IReportService reportService)
        {
            _reportService = reportService;
        }

        [HttpPost("login")]
        public IActionResult Login(loginDto dto)
        {
            if (dto.UserName == "admin" && dto.Password == "admin")
            {
                return Ok("登录成功");
            }
            return Unauthorized("无效的凭证");
        }

        [HttpGet("all")]
        public async Task<ApiResulat> GetAllReports(string? appid="", string? date = "", string? name = "", int state=-1)
        {
            var reports = await _reportService.GetAllReportsAsync( appid,  date,  name,  state);
            if (reports == null)
            {
                return new ApiResulat
                {
                    Data = null,
                    Message = "获取失败",
                    Code = ApiCode.Fail
                };
            }
            return new ApiResulat
            {
                Data = reports,
                Message = "获取成功",
                Code = ApiCode.Success
            };

        }

        [HttpPost("upload")]
        public async Task<ApiResulat> UploadFile([FromForm] IFormFile file, [FromForm] string appId)
        {
            if (file == null || file.Length == 0) { 
                return new ApiResulat { Data = null, Message = "文件为空", Code = ApiCode.Fail }; 
            }

            var wwwrootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
            var appFolderPath = Path.Combine(wwwrootPath, appId);

            if (!Directory.Exists(wwwrootPath))
            {
                Directory.CreateDirectory(wwwrootPath);
            }

            if (!Directory.Exists(appFolderPath))
            {
                Directory.CreateDirectory(appFolderPath);
            }

            var filePath = Path.Combine(appFolderPath, file.FileName);

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(fileStream);
            }

            var fileContent = await System.IO.File.ReadAllBytesAsync(filePath);
          var s=  await _reportService.UploadFileAsync(appId, file.FileName, fileContent);
            if (s == 1)
            {
                return new ApiResulat
                {
                    Data = null,
                    Message = "上传成功",
                    Code = ApiCode.Success
                };
            }
            return new ApiResulat
            {
                Data = null,
                Message = "上传失败",
                Code = ApiCode.Fail
            };

        }

        [HttpPost("update")]
        public async Task<ApiResulat> UpdateReportStatus([FromForm] string appId, [FromForm] string fileName)
        {
            var st = await _reportService.UpdateReportStatusAsync(appId, fileName);
            if (st == 1)
            {
                return new ApiResulat
                {
                    Data = null,
                    Message = "更新成功",
                    Code = ApiCode.Success
                };
            }
            return new ApiResulat
            {
                Data = null,
                Message = "更新失败",
                Code = ApiCode.Fail
            };
        }

        [HttpGet("file")]
        public IActionResult GetFileUrl([FromQuery] string appId, [FromQuery] string fileName)
        {
           
            var fileUrl = $"http://152.136.226.135:8091/{appId}/{fileName}";
            return Ok(fileUrl);
        }
    }
}
